/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dabarobjects.mobile.ui.actions;

import com.dabarobjects.mobile.bluetooth.PhoneBluetoothConnector;
import com.dabarobjects.mobile.bluetooth.SerialPortAgent;
import com.dabarobjects.ui.surfaces.actions.ExtendedUIAction;
import com.dabarobjects.ui.surfaces.actions.NiceOperationWaitingUI;
import java.util.List;
import javax.bluetooth.RemoteDevice;

/**
 *
 * @author DabarObjects Solutions (www.dabarobjects.com)
 */
public class ScanDevicesAction extends ExtendedUIAction {

    private List<RemoteDevice> devices;
    private ActionResultListener<List<RemoteDevice>> actions;

    public ScanDevicesAction(ActionResultListener<List<RemoteDevice>> actions) {
        super("Scan Devices");
        this.actions = actions;
    }

    public ScanDevicesAction() {
        super("Scan Devices");
    }

    @Override
    public void performAction(NiceOperationWaitingUI ops) {
        ops.putMessage("Scanning For Bluetooth Phones");
        try {
            
            PhoneBluetoothConnector cManager = new PhoneBluetoothConnector();

            SerialPortAgent agent = new SerialPortAgent();
            agent.initialize();

            cManager.searchDevices(agent);

            devices = cManager.getDevices();
            this.actions.onActionComplete(devices);
        } catch (Exception e) {
            e.printStackTrace();
            ops.notifyBeforeClosing("Could Not Complete This Operation");
        }
        
        
        ops.complete();
    }

    public List<RemoteDevice> getDevices() {
        return devices;
    }
    
    
}
